package com.company.hrmadmin.modules.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.hrmadmin.common.constant.ErrorEnum;
import com.company.hrmadmin.common.constant.GlobalConstant;
import com.company.hrmadmin.common.exception.BusinessException;
import com.company.hrmadmin.common.exception.GlobalException;
import com.company.hrmadmin.modules.domain.Company;
import com.company.hrmadmin.modules.domain.User;
import com.company.hrmadmin.modules.entity.vo.CompanyVO;
import com.company.hrmadmin.modules.mapper.UserMapper;
import com.company.hrmadmin.modules.service.CompanyService;
import com.company.hrmadmin.modules.mapper.CompanyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

/**
 * @author Xmirror
 * @description 针对表【bs_company(公司)】的数据库操作Service实现
 * @createDate 2022-03-18 15:38:49
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
        implements CompanyService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private CompanyMapper companyMapper;

    @Override
    public Integer getTotalPeople(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new GlobalException(ErrorEnum.USER_NOT_EXIST);
        }
        Integer companyId = user.getCompanyId();
        Long total = userMapper.selectCount(Wrappers.lambdaQuery(User.class)
                .eq(User::getCompanyId, companyId));
        return total.intValue();
    }

    @Override
    public CompanyVO getCompanyVO(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new GlobalException(ErrorEnum.USER_NOT_EXIST);
        }
        Integer companyId = user.getCompanyId();
        Optional.ofNullable(companyId).orElseThrow(() -> new BusinessException(ErrorEnum.COMPANY_INFO_NOT_EXIST));
        Company company = companyMapper.selectById(companyId);
        Optional.ofNullable(company).orElseThrow(() -> new BusinessException(ErrorEnum.COMPANY_INFO_NOT_EXIST));
        SimpleDateFormat sdf = new SimpleDateFormat(GlobalConstant.DATE_FORMAT);
        return new CompanyVO(company.getName(), company.getAddress(), company.getPhone(),
                company.getMail(), company.getIndustry(), sdf.format(company.getCreateTime()),
                sdf.format(company.getStartUseTime()), sdf.format(company.getExpiredTime()));
    }

    @Override
    public Integer getCompanyId(Integer userId) {
        User user = userMapper.selectById(userId);
        Optional.ofNullable(user).orElseThrow(() -> new GlobalException(ErrorEnum.USER_NOT_EXIST));
        Company company = companyMapper.selectById(user.getCompanyId());
        Optional.ofNullable(company).orElseThrow(() -> new GlobalException(ErrorEnum.COMPANY_NOT_EXIST));
        return company.getId();
    }

    @Override
    public List<User> getCompanyUserList(Integer userId) {
        Integer companyId = this.getCompanyId(userId);
        Optional.ofNullable(companyId).orElseThrow(() -> new GlobalException(ErrorEnum.COMPANY_NOT_EXIST));
        return userMapper.selectList(Wrappers.lambdaQuery(User.class)
                .eq(User::getCompanyId, companyId));
    }
}




